package com.qidian.mes.sys.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @Author 戴着假发的程序员
 * @Company 起点编程
 * @Description
 * @Time 2024/10/16 下午 4:20
 */
@Component
public class IDGenUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public String genIfId(){
        StringBuilder sb = new StringBuilder();
        sb.append("IF");
        LocalDate localDate = LocalDate.of(2024,3,6);
        int year = localDate.getYear();
        sb.append(year);
        int monthValue = localDate.getMonthValue();
        sb.append(monthValue < 10 ? ("0" + monthValue) : monthValue);
        int dayOfMonth = localDate.getDayOfMonth();
        sb.append(dayOfMonth < 10 ? ("0" + dayOfMonth): dayOfMonth);
        // 顺位号
        String key = "IFID";
        String idStr = stringRedisTemplate.opsForValue().increment(key) + "";
        // 处理位数
        while(idStr.length() < 5){
            idStr = 0 + idStr;
        }
        sb.append(idStr);
        return sb.toString();
    }

}
